﻿using System;
using System.IO;
using System.Text.RegularExpressions;
using Terraria.Social;

namespace Terraria.Utilities
{
	// Token: 0x020000CE RID: 206
	public static class FileUtilities
	{
		// Token: 0x06000ACC RID: 2764 RVA: 0x0000B855 File Offset: 0x00009A55
		public static bool Exists(string path, bool cloud)
		{
			if (cloud && SocialAPI.Cloud != null)
			{
				return SocialAPI.Cloud.HasFile(path);
			}
			return File.Exists(path);
		}

		// Token: 0x06000ACD RID: 2765 RVA: 0x0000B873 File Offset: 0x00009A73
		public static void Delete(string path, bool cloud)
		{
			if (cloud && SocialAPI.Cloud != null)
			{
				SocialAPI.Cloud.Delete(path);
				return;
			}
			FileOperationAPIWrapper.MoveToRecycleBin(path);
		}

		// Token: 0x06000ACE RID: 2766 RVA: 0x0000B893 File Offset: 0x00009A93
		public static string GetFullPath(string path, bool cloud)
		{
			if (!cloud)
			{
				return Path.GetFullPath(path);
			}
			return path;
		}

		// Token: 0x06000ACF RID: 2767 RVA: 0x0000B8A0 File Offset: 0x00009AA0
		public static void Copy(string source, string destination, bool cloud, bool overwrite = true)
		{
			if (!cloud)
			{
				File.Copy(source, destination, overwrite);
				return;
			}
			if (SocialAPI.Cloud == null || (!overwrite && SocialAPI.Cloud.HasFile(destination)))
			{
				return;
			}
			SocialAPI.Cloud.Write(destination, SocialAPI.Cloud.Read(source));
		}

		// Token: 0x06000AD0 RID: 2768 RVA: 0x0000B8DD File Offset: 0x00009ADD
		public static void Move(string source, string destination, bool cloud, bool overwrite = true)
		{
			FileUtilities.Copy(source, destination, cloud, overwrite);
			FileUtilities.Delete(source, cloud);
		}

		// Token: 0x06000AD1 RID: 2769 RVA: 0x0000B8EF File Offset: 0x00009AEF
		public static int GetFileSize(string path, bool cloud)
		{
			if (cloud && SocialAPI.Cloud != null)
			{
				return SocialAPI.Cloud.GetFileSize(path);
			}
			return (int)new FileInfo(path).Length;
		}

		// Token: 0x06000AD2 RID: 2770 RVA: 0x003E7308 File Offset: 0x003E5508
		public static void Read(string path, byte[] buffer, int length, bool cloud)
		{
			if (cloud && SocialAPI.Cloud != null)
			{
				SocialAPI.Cloud.Read(path, buffer, length);
				return;
			}
			using (FileStream fileStream = File.OpenRead(path))
			{
				fileStream.Read(buffer, 0, length);
			}
		}

		// Token: 0x06000AD3 RID: 2771 RVA: 0x0000B913 File Offset: 0x00009B13
		public static byte[] ReadAllBytes(string path, bool cloud)
		{
			if (cloud && SocialAPI.Cloud != null)
			{
				return SocialAPI.Cloud.Read(path);
			}
			return File.ReadAllBytes(path);
		}

		// Token: 0x06000AD4 RID: 2772 RVA: 0x0000B931 File Offset: 0x00009B31
		public static void WriteAllBytes(string path, byte[] data, bool cloud)
		{
			FileUtilities.Write(path, data, data.Length, cloud);
		}

		// Token: 0x06000AD5 RID: 2773 RVA: 0x003E735C File Offset: 0x003E555C
		public static void Write(string path, byte[] data, int length, bool cloud)
		{
			if (cloud && SocialAPI.Cloud != null)
			{
				SocialAPI.Cloud.Write(path, data, length);
				return;
			}
			string parentFolderPath = FileUtilities.GetParentFolderPath(path, true);
			if (parentFolderPath != "")
			{
				Directory.CreateDirectory(parentFolderPath);
			}
			using (FileStream fileStream = File.Open(path, FileMode.Create))
			{
				while (fileStream.Position < (long)length)
				{
					fileStream.Write(data, (int)fileStream.Position, Math.Min(length - (int)fileStream.Position, 2048));
				}
			}
		}

		// Token: 0x06000AD6 RID: 2774 RVA: 0x0000B93E File Offset: 0x00009B3E
		public static bool MoveToCloud(string localPath, string cloudPath)
		{
			if (SocialAPI.Cloud == null)
			{
				return false;
			}
			FileUtilities.WriteAllBytes(cloudPath, FileUtilities.ReadAllBytes(localPath, false), true);
			FileUtilities.Delete(localPath, false);
			return true;
		}

		// Token: 0x06000AD7 RID: 2775 RVA: 0x0000B95F File Offset: 0x00009B5F
		public static bool MoveToLocal(string cloudPath, string localPath)
		{
			if (SocialAPI.Cloud == null)
			{
				return false;
			}
			FileUtilities.WriteAllBytes(localPath, FileUtilities.ReadAllBytes(cloudPath, true), false);
			FileUtilities.Delete(cloudPath, true);
			return true;
		}

		// Token: 0x06000AD8 RID: 2776 RVA: 0x003E73F0 File Offset: 0x003E55F0
		public static string GetFileName(string path, bool includeExtension = true)
		{
			Match match = FileUtilities.FileNameRegex.Match(path);
			if (match == null || match.Groups["fileName"] == null)
			{
				return "";
			}
			includeExtension &= (match.Groups["extension"] != null);
			return match.Groups["fileName"].Value + (includeExtension ? match.Groups["extension"].Value : "");
		}

		// Token: 0x06000AD9 RID: 2777 RVA: 0x003E7474 File Offset: 0x003E5674
		public static string GetParentFolderPath(string path, bool includeExtension = true)
		{
			Match match = FileUtilities.FileNameRegex.Match(path);
			if (match == null || match.Groups["path"] == null)
			{
				return "";
			}
			return match.Groups["path"].Value;
		}

		// Token: 0x06000ADA RID: 2778 RVA: 0x003E74C0 File Offset: 0x003E56C0
		public static void CopyFolder(string sourcePath, string destinationPath)
		{
			Directory.CreateDirectory(destinationPath);
			foreach (string text in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
			{
				Directory.CreateDirectory(text.Replace(sourcePath, destinationPath));
			}
			foreach (string text2 in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories))
			{
				File.Copy(text2, text2.Replace(sourcePath, destinationPath), true);
			}
		}

		// Token: 0x04000F34 RID: 3892
		private static Regex FileNameRegex = new Regex("^(?<path>.*[\\\\\\/])?(?:$|(?<fileName>.+?)(?:(?<extension>\\.[^.]*$)|$))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
	}
}
